package com.fishcoco.app.yunhang.utils.retrofit.interceptor;

import com.fishcoco.app.yunhang.utils.constant.ConstantUtil;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;

/**
 * Created by app on 2017/1/19.
 */

public class InterceptorAddHeader {
    private static Interceptor interceptor;
    /**
     * 拦截器Interceptors
     * 使用addHeader()不会覆盖之前设置的header,若使用header()则会覆盖之前的header
     *
     * @return
     */
    public static Interceptor getRequestHeader() {
        if (interceptor==null) {
            interceptor = new Interceptor() {

                @Override
                public okhttp3.Response intercept(Chain chain) throws IOException {
                    Request originalRequest = chain.request();
                    Request.Builder builder = originalRequest.newBuilder();
                    builder.header("Authorization", "Bearer " + ConstantUtil.GET_ACCESS_TOKEN);
                    Request.Builder requestBuilder = builder.method(originalRequest.method(), originalRequest.body());
                    Request request = requestBuilder.build();
                    return chain.proceed(request);
                }

            };
        }
        return interceptor;
    }
}
